package cn.com.service.impl;

import cn.com.Util.ConstantUtils;
import cn.com.domain.FileRelationEntity;
import cn.com.domain.FilesEntity;
import cn.com.domain.NoticeEntity;
import cn.com.service.FileRelationService;
import cn.com.service.NoticeService;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.Util.CopyUtils;
import cn.com.domain.dto.NoticeDTO;
import cn.com.mapper.NoticeMapper;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author CRL
 * @Date 2021/4/13 23:19
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeEntity> implements NoticeService {

    @Autowired
    FileRelationService fileRelationService;
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public BaseResponse saveNotice(NoticeDTO requestDto) {
        NoticeEntity noticeEntity = new NoticeEntity();
        CopyUtils.copyProperties(requestDto, noticeEntity);
        noticeEntity.setCreateTime(new Date());
        noticeEntity.setCreateTime(new Date());
        boolean save = this.save(noticeEntity);
        if (save) {
            String id = noticeEntity.getId();
            List<FilesEntity> files = requestDto.getFiles();
            if (!ArrayUtil.isEmpty(files)) {
                for (FilesEntity file : files) {
                    FileRelationEntity relationEntity = new FileRelationEntity();
                    relationEntity.setFileId(String.valueOf(file.getId()));
                    relationEntity.setObjId(String.valueOf(id));
                    relationEntity.setType(ConstantUtils.FILE_TYPE_NOTICE);
                    fileRelationService.save(relationEntity);
                }
            }
            return new BaseResponse(noticeEntity);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    @Override
    public BaseResponse findOneById(String id) {
        NoticeEntity noticeEntity = this.getById(id);
        //封装文件对象
        String objId = noticeEntity.getId();
        List<FilesEntity> files = fileRelationService.findFilesByObjId(objId);
        noticeEntity.setFiles(files);
        return new BaseResponse(noticeEntity);
    }

    @Override
    public BaseResponse deleteNotice(String id) {
        int i = noticeMapper.deleteById(id);
        fileRelationService.deleteByObjId(id);
        if (i == 1) {
            return new BaseResponse(BaseResponseStatus.OK, "删除成功");
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "删除失败");
        }
    }

    @Override
    public BaseResponse updateNotice(String id, NoticeDTO requestDto) {
        Map<String, Object> map = new HashMap(1);
        map.put("id", id);
        List<NoticeEntity> noticeEntities = noticeMapper.selectByMap(map);
        NoticeEntity noticeEntity = noticeEntities.get(0);
        CopyUtils.copyProperties(requestDto, noticeEntity);
        noticeEntity.setUpdateTime(new Date());
        boolean b = this.saveOrUpdate(noticeEntity);
        if (b) {
            fileRelationService.deleteByObjId(id);
            List<FilesEntity> files = requestDto.getFiles();
            if (!ArrayUtil.isEmpty(files)) {
                for (FilesEntity file : files) {
                    FileRelationEntity relationEntity = new FileRelationEntity();
                    relationEntity.setFileId(String.valueOf(file.getId()));
                    relationEntity.setObjId(String.valueOf(id));
                    relationEntity.setType(ConstantUtils.FILE_TYPE_NOTICE);
                    fileRelationService.save(relationEntity);
                }
            }
            return new BaseResponse<>(requestDto);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "更新失败");
        }
    }

    @Override
    public BaseResponse queryNotice(NoticeDTO requestDto) {
        Integer pageNo = requestDto.getPageNo();
        Integer pageSize = requestDto.getPageSize();
        IPage<NoticeEntity> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<NoticeEntity> wrapper = new QueryWrapper<>();
        CopyUtils.copyProperties(requestDto, wrapper);
        IPage<NoticeEntity> noticePage = noticeMapper.selectPage(userPage, wrapper);
        List<NoticeEntity> records = noticePage.getRecords();
        for (NoticeEntity record : records) {
            String id = record.getId();
            List<FilesEntity> files = fileRelationService.findFilesByObjId(id);
            record.setFiles(files);
        }
        return new BaseResponse(noticePage);
    }


}
